/**
 * MCP API Client
 * 
 * Frontend client for MCP server management
 */

const API_BASE_URL = 'http://localhost:3000/api';

export interface McpServer {
  name: string;
  type: 'stdio' | 'sse' | 'http' | 'sdk';
  config: any;
}

export interface McpServerConfig {
  type: 'stdio' | 'sse' | 'http' | 'sdk';
  command?: string;
  args?: string[];
  env?: Record<string, string>;
  url?: string;
  headers?: Record<string, string>;
  name?: string;
  version?: string;
}

export interface Tool {
  name: string;
  description: string;
  inputSchema: any;
}

export interface ApiResponse<T = any> {
  success: boolean;
  data?: T;
  error?: {
    code: string;
    message: string;
  };
  timestamp: string;
}

/**
 * List all MCP servers
 */
export async function listMcpServers(): Promise<McpServer[]> {
  const response = await fetch(`${API_BASE_URL}/mcp/servers`);
  const result: ApiResponse<McpServer[]> = await response.json();
  
  if (!result.success || !result.data) {
    throw new Error(result.error?.message || 'Failed to list MCP servers');
  }
  
  return result.data;
}

/**
 * Get MCP server details
 */
export async function getMcpServer(name: string): Promise<McpServer> {
  const response = await fetch(`${API_BASE_URL}/mcp/servers/${name}`);
  const result: ApiResponse<McpServer> = await response.json();
  
  if (!result.success || !result.data) {
    throw new Error(result.error?.message || 'Failed to get MCP server');
  }
  
  return result.data;
}

/**
 * Register a new MCP server
 */
export async function registerMcpServer(
  name: string,
  config: McpServerConfig
): Promise<void> {
  const response = await fetch(`${API_BASE_URL}/mcp/servers`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ name, config }),
  });
  
  const result: ApiResponse = await response.json();
  
  if (!result.success) {
    throw new Error(result.error?.message || 'Failed to register MCP server');
  }
}

/**
 * Unregister an MCP server
 */
export async function unregisterMcpServer(name: string): Promise<void> {
  const response = await fetch(`${API_BASE_URL}/mcp/servers/${name}`, {
    method: 'DELETE',
  });
  
  const result: ApiResponse = await response.json();
  
  if (!result.success) {
    throw new Error(result.error?.message || 'Failed to unregister MCP server');
  }
}

/**
 * List all available tools
 */
export async function listTools(): Promise<Tool[]> {
  const response = await fetch(`${API_BASE_URL}/tools`);
  const result: ApiResponse<Tool[]> = await response.json();
  
  if (!result.success || !result.data) {
    throw new Error(result.error?.message || 'Failed to list tools');
  }
  
  return result.data;
}

/**
 * Get tool details
 */
export async function getTool(name: string): Promise<Tool> {
  const response = await fetch(`${API_BASE_URL}/tools/${name}`);
  const result: ApiResponse<Tool> = await response.json();
  
  if (!result.success || !result.data) {
    throw new Error(result.error?.message || 'Failed to get tool');
  }
  
  return result.data;
}
